//package com.yunxin.core.application.service.impl;
//
//import cn.hutool.core.date.DateUtil;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.yunxin.common.exception.BusinessException;
//import com.yunxin.common.utils.JacksonUtils;
//import com.yunxin.common.utils.SnowflakeKeyGenerator;
//import com.yunxin.core.application.assembler.OrderAssembler;
//import com.yunxin.core.application.command.order.*;
//import com.yunxin.core.application.dto.order.*;
//import com.yunxin.core.application.seceiver.message.ProductStatistics;
//import com.yunxin.core.application.service.OrderApplicationGatewayService;
//import com.yunxin.core.domain.external.channel.ChannelCustomConfig;
//import com.yunxin.core.domain.external.channel.ChannelExternalService;
//import com.yunxin.core.domain.external.channel.ChannelPromotionPriceInfo;
//import com.yunxin.core.domain.external.product.ProductExternalService;
//import com.yunxin.core.domain.external.product.ProductPriceInfo;
//import com.yunxin.core.domain.external.user.UserExternalService;
//import com.yunxin.core.domain.model.order.*;
//import com.yunxin.core.domain.service.order.OrderService;
//import com.yunxin.core.domain.service.order.impl.OrderStatisticsHandle;
//import com.yunxin.core.domain.service.order.impl.OrderUserApplyAllocationHandle;
//import com.yunxin.core.utils.infra.common.constant.CoreResultCode;
//import com.yunxin.core.utils.infra.common.enums.KeyGeneratorPrefixEnum;
//import com.yunxin.core.utils.infra.utils.AesUtils;
//import com.yunxin.point.domain.external.PointExternalService;
//import com.yunxin.point.domain.external.SaveUserBuryingPointData;
//import com.yunxin.point.infra.common.UserBuryingPointTypeEnum;
//import com.yunxin.statistics.infra.common.ProductStatisticsTypeEnum;
//import com.yunxin.statistics.infra.common.PromotionStatisticsTypeEnum;
//import com.yunxin.statistics.infra.persistence.mapper.UserAllocationStatisticsExtendMapper;
//import lombok.RequiredArgsConstructor;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Service;
//
//import java.util.*;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author zero°
// * @since 2023-07-12
// */
//@Service
//@RequiredArgsConstructor
//public class OrderApplicationGatewayServiceImpl implements OrderApplicationGatewayService {
//
//    private static final int SWITCH = 1;
//
//    private final OrderUserApplyAllocationRepository orderUserApplyAllocationRepository;
//
//    private final OrderService orderService;
//
//    private final OrderUserApplyAllocationHandle orderUserApplyAllocationHandle;
//
//    private final SnowflakeKeyGenerator sequenceGenerator;
//
//    private final ChannelExternalService channelExternalService;
//
//    private final UserExternalService userExternalService;
//
//    private final OrderAssembler orderAssembler;
//
////    private final PointExternalService pointExternalService;
//
//
//
//    @Override
//    public CreateUserApplyOrderDTO createUserApplyOrder(CreateUserApplyOrderCommand command, String clientIp) {
//        ChannelPromotionPriceInfo channelPromotionPriceInfo = channelExternalService.getChannelPromotionPriceInfoByPromotionCode(command.getChannelPromotionCode());
//        if (Objects.isNull(channelPromotionPriceInfo)) {
//            throw new BusinessException(CoreResultCode.CHANNEL_PROMOTION_NOT_EXIST);
//        }
//        // 校验渠道客户配置
//        checkChannelCustomConfig(command, channelPromotionPriceInfo.getPromotionId());
//        String phone = userExternalService.getUserPhoneByUserCode(command.getUserCode());
//        if (Objects.isNull(phone)) {
//            throw new BusinessException(CoreResultCode.USER_NOT_EXIST);
//        }
//        OrderUserApply orderUserApply = orderAssembler.formCreateUserApplyOrderCommand(command);
//        orderUserApply.setUserPhone(phone);
//        orderUserApply.setCity(command.getWorkCity());
//        orderUserApply.setChannelId(channelPromotionPriceInfo.getChannelId());
//        orderUserApply.setApplyOrderNo(sequenceGenerator.generateKey(KeyGeneratorPrefixEnum.ORDER.getPrefix()));
//        orderUserApply.setChannelPromotionPrice(channelPromotionPriceInfo.getChannelPromotionPrice());
//        orderUserApply.setChannelPromotionPriceVersion(channelPromotionPriceInfo.getChannelPromotionPriceVersion());
//        OrderUserApplyDetail orderUserApplyDetail = orderAssembler.formCreateUserApplyOrderCommandForDetail(command);
//        orderUserApplyDetail.setApplyOrderNo(orderUserApply.getApplyOrderNo());
//        orderService.saveOrderUserApply(orderUserApply, orderUserApplyDetail);
//        saveUserBuryingPointData(command.getVisitorId(), clientIp, command.getUserCode(), JacksonUtils.X.format(command), UserBuryingPointTypeEnum.ORDER_APPLY_CREATE);
//        CreateUserApplyOrderDTO dto = new CreateUserApplyOrderDTO();
//        dto.setApplyOrderNo(orderUserApply.getApplyOrderNo());
//        dto.setOrderUserApply(orderUserApply);
//        return dto;
//    }
//
//    /**
//     * 保存埋点数据
//     *
//     * @param visitorId visitorId
//     * @param clientIp  clientIp
//     * @param userCode  userCode
//     * @param data      data
//     * @param typeEnum  typeEnum
//     */
//    private void saveUserBuryingPointData(String visitorId, String clientIp, String userCode, String data, UserBuryingPointTypeEnum typeEnum) {
//        SaveUserBuryingPointData saveUserBuryingPointData = new SaveUserBuryingPointData();
//        saveUserBuryingPointData.setVisitorId(visitorId);
//        saveUserBuryingPointData.setIp(clientIp);
//        saveUserBuryingPointData.setUserCode(userCode);
//        saveUserBuryingPointData.setBuryingPointTypeEnum(typeEnum);
//        saveUserBuryingPointData.setData(data);
//        pointExternalService.saveUserBuryingPointData(saveUserBuryingPointData);
//    }
//
//    @Override
//    public AllocationUserApplyOrderDTO allocationUserApplyOrder(AllocationUserApplyOrderCommand command, String clientIp) {
//        AllocationOrderUserApplyResult result = orderUserApplyAllocationHandle.allocationOrderUserApply(command.getApplyOrderNo(), clientIp);
//        AllocationUserApplyOrderDTO allocationUserApplyOrderDTO = new AllocationUserApplyOrderDTO();
//        allocationUserApplyOrderDTO.setAllocationOrderId(result.getAllocationOrderId());
//        allocationUserApplyOrderDTO.setProductId(result.getProductId());
//        allocationUserApplyOrderDTO.setApiUrl(result.getApiUrl());
//        allocationUserApplyOrderDTO.setIsBottom(result.getIsBottom());
//
//        allocationUserApplyOrderDTO.setUserApply(result.getUserApply());
//        allocationUserApplyOrderDTO.setRedisCashField(result.getRedisCashField());
//        allocationUserApplyOrderDTO.setIsAbutmentType(result.getIsAbutmentType());
//        allocationUserApplyOrderDTO.setProductId(result.getProductId());
//        allocationUserApplyOrderDTO.setOrderUserApplyAllocation(result.getOrderUserApplyAllocation());
//        allocationUserApplyOrderDTO.setClientIp(result.getClientIp());
//        allocationUserApplyOrderDTO.setSex(result.getSex());
//        allocationUserApplyOrderDTO.setZhzxUrl(result.getZhzxUrl());
//        allocationUserApplyOrderDTO.setTotalLimit(result.getTotalLimit());
//        allocationUserApplyOrderDTO.setUserPhone(result.getUserPhone());
//        allocationUserApplyOrderDTO.setProtocol(result.getProtocol());
//        allocationUserApplyOrderDTO.setPlanAuthProtocol(result.getPlanAuthProtocol());
//        return allocationUserApplyOrderDTO;
//    }
//
//    @Override
//    public boolean updateAllocationOrderWechat(UpdateAllocationOrderWechatCommand command) {
//        return orderUserApplyAllocationRepository.updateAllocationOrderWechatById(command.getAllocationOrderId(), command.getUserWechat());
//    }
//
//    @Override
//    public boolean updateAllocationOrderEnterpriseWechat(UpdateAllocationOrderEnterpriseWechatCommand command) {
//        return orderUserApplyAllocationRepository.updateAllocationOrderEnterpriseWechatById(command.getAllocationOrderId(), command.getEnterpriseWechat());
//    }
//
//
//
//
//    /**
//     * 渠道客户配置校验
//     *
//     * @param command     command
//     * @param promotionId promotionId
//     */
//    private void checkChannelCustomConfig(CreateUserApplyOrderCommand command, Long promotionId) {
//        ChannelCustomConfig channelCustomConfig = channelExternalService.getChannelPromotionCustomConfigByPromotionId(promotionId);
//        if (Objects.isNull(channelCustomConfig)) {
//            throw new BusinessException(CoreResultCode.CHANNEL_PROMOTION_CUSTOMER_NOT_EXIST);
//        }
//      /*  if (channelCustomConfig.getIdentityNoStatus() == SWITCH && StringUtils.isBlank(command.getUserIdentityNo())) {
//            throw new BusinessException(CoreResultCode.PARAM_IDENTITY_ERROR);
//        }*/
//        if (channelCustomConfig.getSesameSeedStatus() == SWITCH && Objects.isNull(command.getSesameSeedQuota())) {
//            throw new BusinessException(CoreResultCode.PARAM_SESAME_SEED_ERROR);
//        }
//        if (channelCustomConfig.getCreditCardStatus() == SWITCH && Objects.isNull(command.getCreditCard())) {
//            throw new BusinessException(CoreResultCode.PARAM_CREDIT_CARD_ERROR);
//        }
//        if (channelCustomConfig.getEducationLevelStatus() == SWITCH && Objects.isNull(command.getEducationLevel())) {
//            throw new BusinessException(CoreResultCode.PARAM_EDUCATION_LEVEL_ERROR);
//        }
//        if (channelCustomConfig.getHouseStatus() == SWITCH && Objects.isNull(command.getHouseLevel())) {
//            throw new BusinessException(CoreResultCode.PARAM_HOUSE_ERROR);
//        }
//        if (channelCustomConfig.getVehicleStatus() == SWITCH && Objects.isNull(command.getVehicleLevel())) {
//            throw new BusinessException(CoreResultCode.PARAM_VEHICLE_ERROR);
//        }
//        if (channelCustomConfig.getAccumulationFundStatus() == SWITCH && Objects.isNull(command.getAccumulationFundLevel())) {
//            throw new BusinessException(CoreResultCode.PARAM_ACCUMULATION_FUND_ERROR);
//        }
//        if (channelCustomConfig.getSocialSecurityStatus() == SWITCH && Objects.isNull(command.getSocialSecurityLevel())) {
//            throw new BusinessException(CoreResultCode.PARAM_SOCIAL_SECURITY_ERROR);
//        }
//        if (channelCustomConfig.getInsureStatus() == SWITCH && Objects.isNull(command.getInsure())) {
//            throw new BusinessException(CoreResultCode.PARAM_INSURE_ERROR);
//        }
//        if (channelCustomConfig.getCareerStatus() == SWITCH && Objects.isNull(command.getOccupation())) {
//            throw new BusinessException(CoreResultCode.PARAM_CAREER_ERROR);
//        }
//        if (channelCustomConfig.getMonthlyIncomeStatus() == SWITCH && Objects.isNull(command.getOccupationMonthlyIncome())) {
//            throw new BusinessException(CoreResultCode.PARAM_MONTH_INCOME_ERROR);
//        }
//        if (channelCustomConfig.getJdBtStatus() == SWITCH && Objects.isNull(command.getJdBtQuota())) {
//            throw new BusinessException(CoreResultCode.PARAM_JD_BT_ERROR);
//        }
//        if (channelCustomConfig.getAntCreditPayStatus() == SWITCH && Objects.isNull(command.getAntCreditPayQuota())) {
//            throw new BusinessException(CoreResultCode.PARAM_ANT_CREDIT_PAY_ERROR);
//        }
//        if (channelCustomConfig.getMicroLoanStatus() == SWITCH && Objects.isNull(command.getMicroLoanQuota())) {
//            throw new BusinessException(CoreResultCode.PARAM_MICRO_LOAN_ERROR);
//        }
//        if (channelCustomConfig.getAgeStatus() == SWITCH && Objects.isNull(command.getAge())) {
//            throw new BusinessException(CoreResultCode.PARAM_AGE_ERROR);
//        }
//        if (channelCustomConfig.getSexStatus() == SWITCH && Objects.isNull(command.getSex())) {
//            throw new BusinessException(CoreResultCode.PARAM_SEX_ERROR);
//        }
//        if (channelCustomConfig.getBeOverdueStatus() == SWITCH && Objects.isNull(command.getBeOverdue())) {
//            throw new BusinessException(CoreResultCode.PARAM_BE_OVERDUE_ERROR);
//        }
//        if (channelCustomConfig.getLoanInfoStatus() == SWITCH && Objects.isNull(command.getLoanPeriod())) {
//            throw new BusinessException(CoreResultCode.PARAM_LOAN_PERIOD_ERROR);
//        }
//        if (channelCustomConfig.getLoanInfoStatus() == SWITCH && Objects.isNull(command.getLoanAmount())) {
//            throw new BusinessException(CoreResultCode.PARAM_LOAN_AMOUNT_ERROR);
//        }
//    }
//
//}
